<template>
  <div class="app-container">
    <el-row :gutter="20">

      <!--用户数据-->
      <el-col :span="20" :xs="24">

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                icon="Plus"
                @click="handleAdd"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="success"
                plain
                icon="Edit"
                :disabled="single"
                @click="handleUpdate"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="danger"
                plain
                icon="Delete"
                :disabled="multiple"
                @click="handleDelete"
            >删除</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="positionList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="岗位编号" align="center" key="id" prop="id" />
          <el-table-column label="岗位名称" align="center" key="position" prop="position"  />
          <el-table-column label="岗位薪水" align="center" key="salary" prop="salary" />
          <el-table-column label="岗位状态" align="center" key="status" prop="status" />
<!--          <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">-->
<!--            <template #default="scope">-->
<!--              <el-tooltip content="修改" placement="top" v-if="scope.row.userId !== 1">-->
<!--                <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:user:edit']"></el-button>-->
<!--              </el-tooltip>-->
<!--              <el-tooltip content="删除" placement="top" v-if="scope.row.userId !== 1">-->
<!--                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:user:remove']"></el-button>-->
<!--              </el-tooltip>-->
<!--            </template>-->
<!--          </el-table-column>-->
        </el-table>
      </el-col>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" :rules="rules" ref="positionRef" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="岗位编号" prop="id">
              <el-input v-model="form.id" placeholder="请输入岗位编号" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="岗位名称" prop="position">
              <el-input v-model="form.position" placeholder="请输入岗位名称" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="岗位薪水" prop="salary">
              <el-input v-model="form.salary" placeholder="请输入岗位薪水" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="岗位状态" prop="status">
              <el-input v-model="form.status" placeholder="请输入岗位状态" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="User">
import { getToken } from "@/utils/auth";
import { changeUserStatus, listUser, resetUserPwd, delUser, getUser, updateUser, addUser, deptTreeSelect } from "@/api/system/user";
import {listPositions, addPositions, updatePositions} from "@/api/system/Positions.js";

const router = useRouter();
const { proxy } = getCurrentInstance();

const positionList = ref([]);
const open = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const title = ref("");


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: undefined,
    position: undefined,
    salary: undefined,
    status: undefined
  },
  rules: {
    position: [{ required: true, message: "职位名称不能为空", trigger: "blur" }, { min: 2, max: 20, message: "岗位名称长度必须介于 2 和 20 之间", trigger: "blur" }],
    salary: [{ required: true, message: "岗位薪水不能为空", trigger: "blur" }],
    status: [{ required: true, message: "岗位状态不能为空", trigger: "blur" }],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户列表 */
function getList() {
  listPositions(queryParams).then(res => {
    positionList.value = res.rows;
    console.log(JSON.stringify(res))
    //total.value = res.total;
  });
};


/** 删除按钮操作 */
function handleDelete(row) {
  const userIds = row.userId || ids.value;
  proxy.$modal.confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(function () {
    return delUser(userIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
};




/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};



/** 重置操作表单 */
function reset() {
  form.value = {
    id: undefined,
    position: undefined,
    salary: undefined,
    status: undefined,
  };
  proxy.resetForm("positionRef");
};
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
};
/** 新增按钮操作 */
function handleAdd() {
  open.value = true
  reset();
  // getPositions().then(response => {
  //   postOptions.value = response.posts;
  //   roleOptions.value = response.roles;
  //   open.value = true;
  //   title.value = "添加用户";
  //   form.value.password = initPassword.value;
  // });




};
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const userId = row.userId || ids.value;
  getUser(userId).then(response => {
    form.value = response.data;
    postOptions.value = response.posts;
    roleOptions.value = response.roles;
    form.value.postIds = response.postIds;
    form.value.roleIds = response.roleIds;
    open.value = true;
    title.value = "修改用户";
    form.password = "";
  });
};
/** 提交按钮 */
function submitForm() {
  proxy.$refs["positionRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updatePositions(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPositions(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};

getList();
</script>
